package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"house/models"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *houserpc.CollectAddRequest) (*houserpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line
	var house models.House
	models.DB.Where("id=?", in.HouseId).Find(&house)
	if house.ID == 0 {
		return nil, errors.New("房源不存在")
	}
	collect := models.Collect{
		Model:   gorm.Model{},
		UserId:  int(in.UserId),
		HouseId: int(in.HouseId),
	}
	models.DB.Create(&collect)
	house.CollectCount = house.CollectCount + 1
	models.DB.Updates(&house)
	return &houserpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
